package cn.z2huo.knowledge.concurrency.threadlocal.inheritable;

/**
 * <p>
 *
 * @author z2huo
 */
class InheritableIntegerHolder {

    //private static final ThreadLocal<Integer> INTEGER_THREAD_LOCAL = InheritableThreadLocal.withInitial(() -> 0);

    private static final InheritableThreadLocal<Integer> INTEGER_THREAD_LOCAL = new InheritableThreadLocal<>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public static Integer get() {
        return INTEGER_THREAD_LOCAL.get();
    }

    public static void set(Integer value) {
        INTEGER_THREAD_LOCAL.set(value);
    }

    public static void increment() {
        Integer i = INTEGER_THREAD_LOCAL.get();
        INTEGER_THREAD_LOCAL.set(i + 1);
    }

    public static void decrement() {
        Integer i = INTEGER_THREAD_LOCAL.get();
        INTEGER_THREAD_LOCAL.set(i - 1);
    }

    public static void remove() {
        INTEGER_THREAD_LOCAL.remove();
    }

}
